import request from '@/utils/request';
import { getCaptchaKey } from '@/utils/authority';
import { toFormData } from '@/utils/utils.jsx';
import { stringify } from 'qs';

// =====================用户===========================
//目前的登陆暂时把租户写固定，后期如果有多租户的话再做调整
//账号登陆
export async function accountLogin(params) {
  const values = params;
  values.grant_type = 'captcha';
  values.scope = 'all';
  values.app_code = 'ERP';
  values.tenant_id = '000000';
  values.industrialBeltFlag = INDUSTRIAL_BELT_FLAG;
  return request('/blade-auth/oauth/token', {
    headers: {
      'Tenant-Id': '000000',
      'Captcha-key': getCaptchaKey(),
      'Captcha-code': values.code,
      'Industrial-Belt-Flag': INDUSTRIAL_BELT_FLAG
    },
    method: 'POST',
    body: toFormData(values),
  });
}

//手机号登陆
export async function mobileLogin(params) {
  const values = params;
  values.grant_type = 'sms_code';
  values.scope = 'all';
  values.app_code = 'ERP';
  values.tenant_id = '000000';
  values.industrialBeltFlag = INDUSTRIAL_BELT_FLAG;
  return request('/blade-auth/oauth/token', {
    headers: {
      'Tenant-Id': '000000',
      'UniteUser-Type': 'mobile',
      'Industrial-Belt-Flag': INDUSTRIAL_BELT_FLAG
    },
    method: 'POST',
    body: toFormData(values),
  });
}

//获取图片验证码
export async function getCaptchaImage(params) {
  return request(`/blade-auth/oauth/captcha?${stringify(params)}`);
}

//校验账号
export async function checkAccountOp(params) {
  let headerParams;
  
  const { grantType } = params
  if (grantType == 1){//账号密码登录
    headerParams = {
      'Tenant-Id': '000000',
      'Captcha-key': getCaptchaKey(),
      'Captcha-code': params.code,
      'Industrial-Belt-Flag': INDUSTRIAL_BELT_FLAG
    }
  }else{//手机号登录
    headerParams = {
      'Tenant-Id': '000000',
      'UniteUser-Type': 'mobile',
      'Industrial-Belt-Flag': INDUSTRIAL_BELT_FLAG
    }
  }
  return request(`/blade-user/user/taobao/checkAccount`,{
    headers: headerParams,
    method: 'POST',
    data:params,
  });
}

//二次验证发送验证码
export async function sendCode(params) {
  return request(`/blade-user/user/taobao/idaas/sendCode`, {
    method: 'POST',
    data: params
  })
}

//二次验证输入验证码
export async function checkCode(params) {
  return request(`/blade-user/user/taobao/idaas/checkCode`, {
    method: 'POST',
    data: params
  })
}

//获取安全验证图形验证码
export async function getSaftCaptchaOp(params) {
  return request(`/blade-user/phone/getCodeAndOauth?${stringify(params)}`);
}

//获取微信二维码参数
export async function getWxLoginParamsOp(params) {
  return request(`/blade-user/wx/web/findAppKey?${stringify(params)}`);
}

//微信绑定
export async function bindingMobileOp(params) {
  return request(`/blade-user/user/bindingMobileUnionIdLogin`, {
    method: 'POST',
    body: toFormData(params),
  })
}

//根据微信unionid获取用户列表
export async function getAccountListByUnionIdOp(params) {
  return request(`/blade-user/user/userListByRedis?${stringify(params)}`);
}

//根据手机号获取用户列表
export async function getAccountListByMobileOp(params) {
  return request(`/blade-user/user/findUserListByMobile?${stringify(params)}`);
}

//校验账号/微信登录/手机号登录用
export async function newCheckAccountOp(params) {
  return request(`/blade-user/user/taobao/checkAccount/mobile`,{
    method: "post",
    data: params
  });
}

//根据微信unionid、手机号、appleId获取用户token
export async function findTokenOp(params) {
  return request(`/blade-user/user/findToken?${stringify(params)}`);
}



